<template>
	<view>
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view v-for="(item,index) in cateList" :key="index" @click="activeChanged(index)"
					:class="['left-scroll-view-item',index === active? 'active':'']">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 右侧的滚动视图区域 -->
			<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view class="cate-lv2-view">
					<view class="cate-lv2" v-for="(item,index) in cateLevel2" :key="index">
						<view class="cate-lv2-title" @click="changeLv3(index)"> {{ item.cat_name }} /</view>
					</view>
				</view>
				<!-- 三级分类 列表数据 -->
				<view class="cate-lv3-list">
					<view class="cate-lv3-item" v-for="(item,index) in cateLevel3" :key="index" @click="getGoodsList(item)">
						<image :src="item.cat_icon" mode=""></image>
						<span>{{item.cat_name}}</span>
					</view>

				</view>

			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 当前选中项的索引，默认让第一项被选中
				active: 0,
				wh: 0,
				// 分类数据列表
				cateList: [],
				// 二级分类列表
				cateLevel2: [],
				// 三级分类列表
				cateLevel3: [],
				// 三级分类点击时，查询条件item
				item: {},
			};
		},
		onLoad() {
			// 获取当前系统的信息
			const sysInfo = uni.getSystemInfoSync()
			// 为 wh 窗口可用高度动态赋值
			this.wh = sysInfo.windowHeight
			// 调用获取分类列表数据的方法
			this.getCateList()
		},
		methods: {
			async getCateList() {
				console.log('请求数据开始...');
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				if (res.meta.status != 200) return uni.$showMsg()
				// 数据获取成功
				this.cateList = res.message
				this.cateLevel2 = this.cateList[0].children
				this.cateLevel3 = this.cateLevel2[0].children
				console.log('res.message=', res.message);
			},
			activeChanged(i) {
				this.active = i
				// 改变二级分类的数据
				this.cateLevel2 = this.cateList[i].children
				console.log('this.cateLevel2', this.cateLevel2);
				this.cateLevel3 = this.cateLevel2[0].children
			},
			changeLv3(i) {
				console.log('item level3 event...', i);
				this.cateLevel3 = this.cateLevel2[i].children
			},
			// 获取符合条件商品的列表
			getGoodsList(item) {
				console.log('item',item);
				this.item = item
				uni.navigateTo({
					url: '/pages/goods_list/goods_list?cid='+item.cat_id
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				// 激活项的样式
				&.active {
					background-color: #ffffff;
					position: relative;

					// 渲染激活项左侧的红色指示边线
					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}
	}

	.cate-lv2-view {
		display: flex;
	}

	.cate-lv2-title {
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
	}

	.cate-lv3-list {
		display: flex;
		flex-wrap: wrap;

		.cate-lv3-item {
			width: 33.33%;
			margin-bottom: 10px;
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 60px;
				height: 60px;
			}

			text {
				font-size: 12px;
			}
		}
	}
</style>